<template>
  <el-dialog :title="form.id ? $t('btn.edit') : $t('operation.add')" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="100px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb-6">
          <el-form-item :label="$t('relationD.checkProjectName')" prop="checkProjectName" >
            <el-select :placeholder="$t('relationD.checkProjectName')"
                       filterable
                       remote
                       @change="changeCheckProject"
                       :remote-method="getCheckProject"
                       :loading = "CheckProjectLoading"
                       v-model="form.checkProjectName">
              <el-option v-for="item in checkProjectList"
                         :key="item.checkProjectName"
                         :label="item.checkProjectName"
                         :value="item">
                <span style="float: left">{{ item.checkProjectName }}</span>
                <span style=" float: right;  color: var(--el-text-color-secondary);  font-size: 13px; "> {{
                    item.checkProjectCode
                  }} </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb-6">
          <el-form-item :label="$t('relationD.checkProjectCode')" prop="checkProjectCode" >
            <el-input disabled :placeholder="$t('relationD.checkProjectCode')" v-model="form.checkProjectCode"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb-6">
          <el-form-item :label="$t('relationD.resultType')" prop="resultType" >
            <el-input disabled :placeholder="$t('relationD.resultType')" v-model="form.resultType"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb-6">
          <el-form-item :label="$t('relationD.standardItem')" prop="standardItem" >
            <el-input disabled :placeholder="$t('relationD.standardItem')" v-model="form.standardItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb-6">
          <el-form-item :label="$t('relationD.unit')" prop="unit" >
            <el-input disabled :placeholder="$t('relationD.unit')" v-model="form.unit"/>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="QmsRelationDDialog">
import {useMessage} from "/@/hooks/message";
import {addObj, getObj, putObj} from '/@/api/platform/qms/qmsRelationD'
import {qmsCheckProjectDetailsPage} from "/@/api/platform/qms/qmsCheckProjectDetails";
import {useI18n} from "vue-i18n";

const {t} = useI18n();

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典
const testTypeList = ref();
const checkProjectList = ref();

// 提交表单数据
const form = reactive({
  id: '',
  masterId: '',
  relationItemCode: '',
  checkProjectName: '',
  checkProjectCode: '',
  testTypeName: '',
  testTypeCode: '',
  standardItem: '',
  unit: '',
  resultType: '',
  creationTime: '',
  createUserNo: '',
  createUserName: '',
  lastModifyTime: '',
  lastModifyUserName: '',
  lastModifyUserNo: '',
  attribute1: '',
  attribute2: '',
  attribute3: '',
});

//父组件的值
const faData = ref();
// 检测项目加载状态
const CheckProjectLoading = ref(true);


const getCheckProject = (query: string) => {
  CheckProjectLoading.value = true
  qmsCheckProjectDetailsPage(
      {"checkProjectName": query, "checkTypeCode": faData.value.checkType}
  ).then((res: any) => {
    if (res.code == 0) {
      checkProjectList.value = res.data.records
      CheckProjectLoading.value = false
    }
  })
}

const changeCheckProject = (val: any) => {
  form.checkProjectName = val.checkProjectName
  form.checkProjectCode = val.checkProjectCode
  form.resultType = val.resultType
  form.standardItem = val.standardItem
  form.unit = val.unit
  form.attribute1 = val.id
}


// 定义校验规则
const dataRules = ref({
  testTypeName: [
    {required: true, trigger: 'blur'}
  ],
  checkProjectName: [
    {required: true, trigger: 'blur'}
  ],
})

// 打开弹窗
const openDialog = async (childRow: any, parentRow: any) => {
  faData.value = parentRow
  form.id = undefined

  // 重置表单数据
  await nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 判断是新增还是编辑的方法
  if (childRow?.id) {
    // 编辑操作
    Object.assign(form, childRow);
  } else {
    // 新增操作 如果三个个属性为空，则提示错误
    if (!parentRow?.id) {
      // 提示错误
      return useMessage().error(t('qms.pleaseSelectTestData'));
    }
    form.masterId = parentRow?.id;
    form.relationItemCode = parentRow?.relationItemCode;
  }
  visible.value = true;
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? t('qms.modifiedSuccessfully') : t('qms.addSuccessfully'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getqmsRelationDData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>